home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / CaptainPlanetHD / src / diskreader.asm next >
Assembly Source File  |  2000-05-11  |  11KB  |  530 lines

  1. ; diskreader.asm - essential functionality for game disk installers.
  2. ; © 1998-2000 Kyzer/CSG
  3.  
  4. ; FILEMODE means that no output diskfile will be used, therefore WRITE is
  5. ; dropped - instead, SAVEF is used to write individual files
  6.     IFND    FILEMODE
  7. FILEMODE=0
  8.     ENDC
  9.  
  10. ; TRACKMODE means that all tracks are DOS tracks, so the user specifies
  11. ; the trackdisk-like device on the command line, and RAWREAD/RESYNC are 
  12. ; dropped
  13.     IFND    TRACKMODE
  14. TRACKMODE=0
  15.     ENDC
  16.  
  17. ; MESSAGES means that each track read will print out its number for the
  18. ; user to see.
  19.     IFND    MESSAGES
  20. MESSAGES=0
  21.     ENDC
  22.  
  23. ; NO_INCLUDES means that no system includes are neccessary to use diskreader
  24.     IFD    NO_INCLUDES
  25. IO_COMMAND=28            ; from devices/trackdisk.i
  26. IO_FLAGS=30
  27. IO_LENGTH=36
  28. IO_DATA=40
  29. IO_OFFSET=44
  30. IOTD_SIZE=56
  31. CMD_READ=2
  32. TD_MOTOR=9
  33. TD_RAWREAD=16
  34. IOTDB_INDEXSYNC=4
  35. MODE_NEWFILE=1006        ; from dos/dos.i
  36. ERROR_NOT_A_DOS_DISK=225
  37. _LVOOpen=-30            ; from dos/dos_lib.i
  38. _LVOClose=-36
  39. _LVOWrite=-48
  40. _LVOIoErr=-132
  41. _LVOSetIoErr=-462
  42. _LVOPrintFault=-474
  43. _LVOReadArgs=-798
  44. _LVOFreeArgs=-858
  45. _LVOPutStr=-948
  46. _LVOVPrintf=-954
  47. _LVOCloseLibrary=-414        ; from exec/exec_lib.i
  48. _LVOOpenDevice=-444
  49. _LVOCloseDevice=-450
  50. _LVODoIO=-456
  51. _LVOOpenLibrary=-552
  52. _LVOCreateIORequest=-654
  53. _LVODeleteIORequest=-660
  54. _LVOCreateMsgPort=-666
  55. _LVODeleteMsgPort=-672
  56.     ELSE
  57.     include    devices/trackdisk.i
  58.     include    dos/dos.i
  59.     include    dos/dos_lib.i
  60.     include    exec/exec_lib.i
  61.     ENDC
  62.  
  63. DOSTRACKLEN=512*11
  64.  
  65. BUFFER    MACRO    ; buffername
  66. \1    equ    __trk
  67.     ENDM
  68.  
  69. FAILURE    MACRO    ; [reason]
  70.     IFEQ    NARG
  71.     suba.l    a0,a0
  72.     ELSE
  73.     lea    \1,a0
  74.     ENDC
  75.     bra    __fail
  76.     ENDM
  77.  
  78.     IFEQ    TRACKMODE
  79. RAWREAD    MACRO    ; track
  80.     lea    __trk,a0
  81.     move.l    \1,d0
  82.     bsr    __rawrd
  83.     ENDM
  84. RESYNC    MACRO    ; wordsync
  85.     lea    __trk,a0
  86.     move.l    \1,d0
  87.     bsr    __sync
  88.     ENDM
  89.     ENDC
  90. DOSREAD    MACRO    ; track
  91.     lea    __trk,a0
  92.     move.l    \1,d0
  93.     bsr    __dosrd
  94.     ENDM
  95.  
  96.     IFEQ    FILEMODE
  97. WRITE    MACRO    ; length, [offset]
  98.     IFEQ    NARG-2
  99.     lea    __trk,a0
  100.     add.l    \2,a0
  101.     ELSE
  102.     lea    __trk,a0
  103.     ENDC
  104.     move.l    \1,d0
  105.     bsr    __write
  106.     ENDM
  107. WRITEDOS MACRO    ; track
  108.     DOSREAD    \1
  109.     WRITE    #DOSTRACKLEN
  110.     ENDM
  111.  
  112.     ELSE
  113. SAVEF    MACRO    ; filename, buffer, length
  114.     lea    \1,a0
  115.     lea    \2,a1
  116.     move.l    \3,d0
  117.     bsr    __savef
  118.     ENDM
  119.     ENDC
  120.  
  121. ;------------------------------------
  122.  
  123. call    macro
  124.     jsr    _LVO\1(a6)
  125.     endm
  126.  
  127. initstk    macro    ; stack_symbol, stackreg
  128.     link    \2,#\1
  129.     move.l    sp,a0
  130. .clr\@    clr.w    (a0)+
  131.     cmp.l    a0,\2
  132.     bne.s    .clr\@
  133.     endm
  134.  
  135. stackf    MACRO    ; stack_symbol, stackelement_symbol, [size=4]
  136.     IFND    \1
  137. \1    set    0
  138.     ENDC
  139.     IFGE    NARG-3
  140. \1    set    \1-(\3)
  141.     ELSE
  142. \1    set    \1-4
  143.     ENDC
  144. \2    equ    \1
  145.     ENDM
  146.  
  147. ; create appropriate command line arguments based on FILEMODE/TRACKMODE
  148.     IFNE    FILEMODE
  149.     IFNE    TRACKMODE
  150.  
  151.     ; filemode on, trackmode on
  152.     stackf    stk, __unit
  153.     stackf    stk, __device
  154. __args=__device
  155. __nargs=2
  156. __tmpl    macro
  157.     dc.b    "DEVICE/A,UNIT/N/A",0
  158.     endm
  159.  
  160.     ELSE
  161.  
  162.     ; filemode on, trackmode off
  163.     stackf    stk, __unit
  164. __args=__unit
  165. __nargs=1
  166. __tmpl    macro
  167.     dc.b    "UNIT/N/A",0
  168.     endm
  169.  
  170.     ENDC
  171.     ELSE
  172.     IFNE    TRACKMODE
  173.  
  174.     ; filemode off, trackmode on
  175.     stackf    stk, __unit
  176.     stackf    stk, __device
  177.     stackf    stk, __output
  178. __args=__output
  179. __nargs=3
  180. __tmpl    macro
  181.     dc.b    "DISKFILE/A,DEVICE/A,UNIT/N/A",0
  182.     endm
  183.  
  184.     ELSE
  185.  
  186.     ; filemode off, trackmode off
  187.     stackf    stk, __unit
  188.     stackf    stk, __output
  189. __args=__output
  190. __nargs=2
  191. __tmpl    macro
  192.     dc.b    "DISKFILE/A,UNIT/N/A",0
  193.     endm
  194.  
  195.     ENDC
  196.     ENDC
  197.  
  198. ; other variables used
  199.  
  200.     stackf    stk, __rdargs    ; returned by ReadArgs()
  201.     stackf    stk, __diskport    ; replyport for diskio
  202.     stackf    stk, __diskio    ; IORequest to trackdisk.device
  203.     stackf    stk, __outfh    ; output filehandle (NULL in filemode)
  204.     stackf    stk, __initsp    ; initial (sp): move to sp then rts to quit
  205.     stackf    stk, __reason    ; ptr to textual reason for failure, or NULL
  206.     stackf    stk, __ioerr    ; a particular error code, overriding IoErr()
  207.     stackf    stk, execbase    ; exec.library
  208.     stackf    stk, dosbase    ; dos.library
  209.  
  210. ;------------------------------------
  211.  
  212.     section    diskreader,code
  213.     link    a5,#stk
  214.     move.l    4.w,a6
  215.     move.l    a6,execbase(a5)
  216.  
  217. ; no printable reason for failure, but start with errorcode
  218. ; incase we can't open DOS
  219.  
  220.     clr.l    __reason(a5)
  221.     clr.l    __ioerr(a5)
  222.     moveq    #100,d7
  223.  
  224. ; open dos.library
  225.     moveq    #37,d0
  226.     lea    __dosnm(pc),a1
  227.     call    OpenLibrary
  228.     move.l    d0,dosbase(a5)
  229.     beq    .nodos
  230.     move.l    d0,a6
  231.  
  232. ; read arguments on command line
  233.     lea    __templ(pc),a0
  234.     move.l    a0,d1
  235.     lea    __args(a5),a0
  236.     move.l    a0,d2
  237.     REPT    __nargs
  238.     clr.l    (a0)+
  239.     ENDR
  240.     moveq    #0,d3
  241.     call    ReadArgs
  242.     move.l    d0,__rdargs(a5)
  243.     beq    .noargs
  244.  
  245. ; create the output diskfile (unless FILEMODE)
  246.     IFEQ    FILEMODE
  247.     move.l    __output(a5),d1
  248.     move.l    #MODE_NEWFILE,d2
  249.     call    Open
  250.     move.l    d0,__outfh(a5)
  251.     beq.s    .nofile
  252.     ENDC
  253.  
  254. ; open trackdisk.device (or user-specified in TRACKMODE)
  255.     move.l    #ERROR_DEVICE_NOT_MOUNTED,__ioerr(a5)
  256.     move.l    execbase(a5),a6
  257.     call    CreateMsgPort
  258.     move.l    d0,__diskport(a5)
  259.     beq.s    .noport
  260.  
  261.     move.l    d0,a0
  262.     moveq    #IOTD_SIZE,d0
  263.     call    CreateIORequest
  264.     move.l    d0,__diskio(a5)
  265.     beq.s    .noio
  266.  
  267.     move.l    d0,a1
  268.     move.l    __unit(a5),a0
  269.     move.l    (a0),d0
  270.     IFNE    TRACKMODE
  271.     move.l    __device(a5),a0
  272.     ELSE
  273.     lea    __tdnm(pc),a0
  274.     ENDC
  275.     moveq    #0,d1
  276.     call    OpenDevice
  277.     tst.l    d0
  278.     bne.s    .nodev
  279.     clr.l    __ioerr(a5)
  280.  
  281. ;--------------------------------------
  282. ; call and return from the main 'slave'
  283.     bsr    __main
  284. ;--------------------------------------
  285.  
  286.  
  287. ; if messages mode, advance to new line for clarity
  288.     IFNE    MESSAGES
  289.     pea    10<<24    ; "\n\0\0\0"
  290.     move.l    sp,d1
  291.     move.l    dosbase(a5),a6
  292.     call    PutStr    ; print a newline
  293.     addq.l    #4,sp
  294.     ENDC
  295.  
  296. ; turn off disk motor
  297.     move.l    __diskio(a5),a1
  298.     move.w    #TD_MOTOR,IO_COMMAND(a1)
  299.     clr.l    IO_LENGTH(a1)
  300.     move.l    execbase(a5),a6
  301.     call    DoIO
  302.     
  303. ; close disk device
  304.     move.l    __diskio(a5),a1
  305.     call    CloseDevice
  306. .nodev    move.l    __diskio(a5),a0
  307.     call    DeleteIORequest
  308. .noio    move.l    __diskport(a5),a0
  309.     call    DeleteMsgPort
  310. .noport
  311.  
  312.     move.l    dosbase(a5),a6
  313.  
  314. ; close the output diskfile (if not FILEMODE)
  315.     IFEQ    FILEMODE
  316.     move.l    __outfh(a5),d1
  317.     call    Close
  318. .nofile
  319.     ENDC
  320.  
  321. ; free command-line arguments
  322.     move.l    __rdargs(a5),d1
  323.     call    FreeArgs
  324. .noargs
  325.  
  326. ; print error message if ioerror - this includes NOT_A_DOS_DISK
  327. ; if a printable reason exists, use that as the head of the printed
  328. ; error message.
  329.  
  330.     moveq    #0,d7         ; returncode = 0
  331.     move.l    __ioerr(a5),d1
  332.     bne.s    1$
  333.     call    IoErr
  334.     move.l    d0,d1
  335.     beq.s    2$
  336. 1$    moveq    #20,d7        ; returncode = 20
  337. 2$    move.l    __reason(a5),d2
  338.     call    PrintFault
  339.  
  340. ; close dos.library and go home
  341.     move.l    a6,a1
  342.     move.l    execbase(a5),a6
  343.     call    CloseLibrary
  344. .nodos    move.l    d7,d0
  345.     unlk    a5
  346.     rts
  347.  
  348.  
  349. ; internal routine to print out track number - D0 = track
  350.     IFNE    MESSAGES
  351. __prtrk    movem.l    d0-d2/a0,-(sp)
  352.     lea    __msg(pc),a0
  353.     move.l    a0,d1
  354.     move.l    sp,d2    ; points at D0 on the stack
  355.     move.l    dosbase(a5),a6
  356.     call    VPrintf
  357.     movem.l    (sp)+,d0-d2/a0
  358.     rts
  359.     ENDC
  360.  
  361. ; if TRACKMODE, then use a complete DOSREAD routine
  362.  
  363.     IFNE    TRACKMODE
  364. ;------------------------------------
  365. ; a0 = buffer, d0 = track
  366. __dosrd    move.l    a6,-(sp)
  367.     IFNE    MESSAGES
  368.     bsr.s    __prtrk        ; print track number in messages mode
  369.     ENDC
  370.     move.l    __diskio(a5),a1
  371.     move.w    #CMD_READ,IO_COMMAND(a1)
  372.     move.l    #DOSTRACKLEN,d1
  373.     mulu    d1,d0            ; convert D0=track to D0=offset
  374.     move.l    d1,IO_LENGTH(a1)    ; D1 = length
  375.  
  376.     move.l    a0,IO_DATA(a1)
  377.     move.l    d0,IO_OFFSET(a1)
  378.     move.l    execbase(a5),a6
  379.     call    DoIO            ; read disk part
  380.     lea    __ertrk(pc),a0        ; fail with "error reading track"
  381.     tst.l    d0            ; if DoIO fails
  382.     bne.s    __fail    
  383.     move.l    (sp)+,a6
  384.     rts
  385.     ELSE
  386.  
  387. ; if not TRACKMODE, merge the common parts of RAWREAD and DOSREAD
  388.  
  389. ;------------------------------------
  390. ; a0 = buffer, d0 = track
  391. __dosrd    move.l    a6,-(sp)
  392.     IFNE    MESSAGES
  393.     bsr.s    __prtrk
  394.     ENDC
  395.     move.l    __diskio(a5),a1
  396.     move.w    #CMD_READ,IO_COMMAND(a1)
  397.     move.l    #DOSTRACKLEN,d1
  398.     mulu    d1,d0            ; as above, D0 = offset, D1 = length
  399.     move.l    d1,IO_LENGTH(a1)
  400.     bra.s    __rdcom
  401.  
  402. ;------------------------------------
  403. ; a0 = buffer, d0 = track
  404. __rawrd    move.l    a6,-(sp)
  405.     IFNE    MESSAGES
  406.     bsr.s    __prtrk
  407.     ENDC
  408.     move.l    __diskio(a5),a1
  409.     move.w    #TD_RAWREAD,IO_COMMAND(a1)
  410.     move.b    #IOTDB_INDEXSYNC,IO_FLAGS(a1)    ; just for fun...
  411.     move.l    #$7ffe,IO_LENGTH(a1)    ; here length always is maximum
  412.  
  413. __rdcom    move.l    a0,IO_DATA(a1)
  414.     move.l    d0,IO_OFFSET(a1)
  415.     move.l    execbase(a5),a6
  416.     call    DoIO            ; error handling as above
  417.     lea    __ertrk(pc),a0
  418.     tst.l    d0
  419.     bne.s    __fail
  420.     move.l    (sp)+,a6
  421.     rts
  422.     ENDC
  423.  
  424. ; FAIL will always quit out of the 'slave' and return to the main
  425. ; routine, whatever the location on the stack
  426.  
  427. ;------------------------------------
  428. ; a0 = failure reason
  429. __fail    move.l    a0,d0
  430.     beq.s    .noreas
  431.     move.l    d0,__reason(a5)
  432.     move